﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.House.DAL
{
    /// <summary>
    /// 数据访问类Community_BuildingInfo。
    /// </summary>
    public class Community_BuildingInfo
    {
        public Community_BuildingInfo()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid BuildingId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Community_BuildingInfo where BuildingId=@BuildingId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "BuildingId", DbType.Guid, BuildingId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(string strwhere)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            string strSql = string.Format("select count(1) from Community_BuildingInfo where {0}", strwhere);
            DbCommand dbCommand = db.GetSqlStringCommand(strSql);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.House.Entity.Community_BuildingInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Community_BuildingInfo(");
            strSql.Append("BuildingId,CommunityId,BuildingName,Floor,UnitNum,UnitType,RoomRule,RoomRuleRemark,AddTime,AddPerson,Status)");

            strSql.Append(" values (");
            strSql.Append("@BuildingId,@CommunityId,@BuildingName,@Floor,@UnitNum,@UnitType,@RoomRule,@RoomRuleRemark,@AddTime,@AddPerson,@Status)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "BuildingId", DbType.Guid, model.BuildingId);
            db.AddInParameter(dbCommand, "CommunityId", DbType.Guid, model.CommunityId);
            db.AddInParameter(dbCommand, "BuildingName", DbType.AnsiString, model.BuildingName);
            db.AddInParameter(dbCommand, "Floor", DbType.Int32, model.Floor);
            db.AddInParameter(dbCommand, "UnitNum", DbType.Int32, model.UnitNum);
            db.AddInParameter(dbCommand, "UnitType", DbType.Int32, model.UnitType);
            db.AddInParameter(dbCommand, "RoomRule", DbType.AnsiString, model.RoomRule);
            db.AddInParameter(dbCommand, "RoomRuleRemark", DbType.AnsiString, model.RoomRuleRemark);
            db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            db.AddInParameter(dbCommand, "AddPerson", DbType.AnsiString, model.AddPerson);
            db.AddInParameter(dbCommand, "Status", DbType.Int32, model.Status);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.House.Entity.Community_BuildingInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Community_BuildingInfo set ");
            strSql.Append("CommunityId=@CommunityId,");
            strSql.Append("BuildingName=@BuildingName,");
            strSql.Append("Floor=@Floor,");
            strSql.Append("UnitNum=@UnitNum,");
            strSql.Append("UnitType=@UnitType,");
            strSql.Append("RoomRule=@RoomRule,");
            strSql.Append("RoomRuleRemark=@RoomRuleRemark,");
            strSql.Append("AddTime=@AddTime,");
            strSql.Append("AddPerson=@AddPerson,");
            strSql.Append("Status=@Status");
            strSql.Append(" where BuildingId=@BuildingId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "BuildingId", DbType.Guid, model.BuildingId);
            db.AddInParameter(dbCommand, "CommunityId", DbType.Guid, model.CommunityId);
            db.AddInParameter(dbCommand, "BuildingName", DbType.AnsiString, model.BuildingName);
            db.AddInParameter(dbCommand, "Floor", DbType.Int32, model.Floor);
            db.AddInParameter(dbCommand, "UnitNum", DbType.Int32, model.UnitNum);
            db.AddInParameter(dbCommand, "UnitType", DbType.Int32, model.UnitType);
            db.AddInParameter(dbCommand, "RoomRule", DbType.AnsiString, model.RoomRule);
            db.AddInParameter(dbCommand, "RoomRuleRemark", DbType.AnsiString, model.RoomRuleRemark);
            db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            db.AddInParameter(dbCommand, "AddPerson", DbType.AnsiString, model.AddPerson);
            db.AddInParameter(dbCommand, "Status", DbType.Int32, model.Status);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="buildingId"></param>
        /// <param name="status"></param>
        public static void UpdateStatus(Guid buildingId, int status)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Community_BuildingInfo set ");
            strSql.Append("Status=@Status");
            strSql.Append(" where BuildingId=@BuildingId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "BuildingId", DbType.Guid, buildingId);
            db.AddInParameter(dbCommand, "Status", DbType.Int32, status);
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid BuildingId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Community_BuildingInfo ");
            strSql.Append(" where BuildingId=@BuildingId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "BuildingId", DbType.Guid, BuildingId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.House.Entity.Community_BuildingInfo GetModel(Guid BuildingId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select BuildingId,CommunityId,BuildingName,Floor,UnitNum,UnitType,RoomRule,RoomRuleRemark,AddTime,AddPerson,Status from Community_BuildingInfo ");
            strSql.Append(" where BuildingId=@BuildingId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "BuildingId", DbType.Guid, BuildingId);
            Chenxin.Lib.House.Entity.Community_BuildingInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select BuildingId,CommunityId,BuildingName,Floor,UnitNum,UnitType,RoomRule,AddTime,AddPerson,Status ");
            strSql.Append(" FROM Community_BuildingInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere + " order by BuildingName");
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得指定列查询结果
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <param name="columnString">查询列</param>
        /// <returns></returns>
        public static DataTable GetList(string strWhere,string columnString)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("select {0} ", columnString);
            strSql.Append(" FROM Community_BuildingInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere + " order by BuildingName");
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString()).Tables[0];
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "Community_BuildingInfo");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.House.Entity.Community_BuildingInfo> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select BuildingId,CommunityId,BuildingName,Floor,UnitNum,UnitType,RoomRule,AddTime,AddPerson,Status ");
            strSql.Append(" FROM Community_BuildingInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.House.Entity.Community_BuildingInfo> list = new List<Chenxin.Lib.House.Entity.Community_BuildingInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.House.Entity.Community_BuildingInfo> GetListArray(string strWhere, string strOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select BuildingId,CommunityId,BuildingName,Floor,UnitNum,UnitType,RoomRule,AddTime,AddPerson,Status ");
            strSql.Append(" FROM Community_BuildingInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            if (strOrder.Trim() != "")
            {
                strSql.Append(" order by " + strOrder + " ASC");
            }
            List<Chenxin.Lib.House.Entity.Community_BuildingInfo> list = new List<Chenxin.Lib.House.Entity.Community_BuildingInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }

        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.House.Entity.Community_BuildingInfo ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.House.Entity.Community_BuildingInfo model = new Chenxin.Lib.House.Entity.Community_BuildingInfo();
            object ojb;
            ojb = dataReader["BuildingId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BuildingId = new Guid(ojb.ToString());
            }
            ojb = dataReader["CommunityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CommunityId = new Guid(ojb.ToString());
            }
            model.BuildingName = dataReader["BuildingName"].ToString();
            ojb = dataReader["Floor"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Floor = (int)ojb;
            }
            ojb = dataReader["UnitNum"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.UnitNum = (int)ojb;
            }
            ojb = dataReader["UnitType"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.UnitType = (int)ojb;
            }
            model.RoomRule = dataReader["RoomRule"].ToString();
            ojb = dataReader["AddTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddTime = (DateTime)ojb;
            }
            model.AddPerson = dataReader["AddPerson"].ToString();


            model.RoomRuleRemark = dataReader["RoomRuleRemark"].ToString();
            ojb = dataReader["Status"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Status = (int)ojb;
            }
            return model;
        }

        #endregion  成员方法
    }
}

